package cn.itcast.controller;



import cn.itcast.domain.Area;

import cn.itcast.service.AreaService;

import cn.itcast.utils.PinYin4jUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AreaController {


    @Autowired
    private AreaService areaService;

    /**
     * 添加
     */

    @RequestMapping("/area_save")
    public String save(Area area){
        areaService.save(area);
        return "redirect:/pages/base/area.html";


    }

    /**
     * 批量导入
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/area_batchImport")
    public String areaSave(MultipartFile file) throws IOException {
        //创建一个集合
        List<Area> areas = new ArrayList<>();
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(file.getInputStream());
        //读取sheet（文件）
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        //读取每一行
        for (Row row : sheet) {
            //一行一个数据 对应的 就是一个区域对象
            if (row.getRowNum() == 0) {
                //如果是第一行就把它跳过
                continue;
            }
            //判断是不是空行跳过
            if (row.getCell(0) == null|| StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
                continue;
            }
            //创建对象
            Area area=new Area();
            //设置参数
            area.setId(row.getCell(0).getStringCellValue());
            area.setProvince(row.getCell(1).getStringCellValue());
            area.setCity(row.getCell(2).getStringCellValue());
            area.setDistrict(row.getCell(3).getStringCellValue());
            area.setPostcode(row.getCell(4).getStringCellValue());
            //设置编码和简码
            String province = area.getProvince();
            String city = area.getCity();
            String district = area.getDistrict();
            //只需要城市的名字  不需要后面带市
            province.substring(0,province.length()-1);
            city.substring(0,city.length()-1);
            district.substring(0,district.length()-1);
           //设置简码
            String[] headByString = PinYin4jUtils.getHeadByString(province + city + district);
            StringBuffer stringBuffer=new StringBuffer();
            for (String s : headByString) {
                stringBuffer.append(s);
            }
            String s = stringBuffer.toString();
            area.setShortcode(s);
            //设置城市编码
            String citys = PinYin4jUtils.hanziToPinyin(city, "");
            area.setCitycode(citys);
            areas.add(area);
        }


        areaService.save( areas);
        return "redirect:/pages/base/area.html";

    }

    @RequestMapping("/area_pageQuery")
    @ResponseBody
    public Map pageQuery(int page, int rows,Area area) {
        Page<Area> pageResult = areaService.pageQuery(page, rows,area);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageResult.getTotalElements());
        result.put("rows", pageResult.getContent());

        return result;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/area_delBatch")
    public String areaDelBatch(String ids) {
        areaService.areaDelBatch(ids);

        return "redirect:/pages/base/area.html";
    }


}
